package chapter14;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/3/13 13:46
 */
interface SomeMethods {
    void boring1();

    void boring2();

    void interesting(String arg);

    void boring3();
}

class Implementation implements SomeMethods {

    @Override
    public void boring1() {
        System.out.println("boring1");
    }

    @Override
    public void boring2() {
        System.out.println("boring2");
    }

    @Override
    public void interesting(String arg) {
        System.out.println("interesting");
    }

    @Override
    public void boring3() {
        System.out.println("boring3");
    }
}

class MethodSelector implements InvocationHandler {

    private Object target;

    public MethodSelector(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if (method.getName().contains("interest")) {
            System.out.println(method.getName());
        }
        return method.invoke(target, args);
    }
}

public class SelectingMethods {
    public static void main(String[] args) {
        SomeMethods someMethods = (SomeMethods) Proxy.newProxyInstance(SelectingMethods.class.getClassLoader(), new Class[]{SomeMethods.class}, new MethodSelector(new Implementation()));
        someMethods.boring1();
        someMethods.interesting("aa");
    }
}
